// pages/search/search.js
var app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		nextPageUrl: true,
		pageNum: 1,
		positions: [],
    scrollTop:0,

		iconClose: true,
		likeHidden: false,
		nullHidden: true,
		searchFocus: true,
		city:"全国"
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		
		this.setData({
			winHeight: app.globalData.systemInfo.windowHeight,
			backgroundColor: '#fff',
			
		})
		this.searchRecommend();

    if(options.city){
      this.setData({city:options.city});
    }

		if(options.value){
			this.setData({
				searchVal: options.value,
			})
			this.searchConfirm();
		}
	},

	//猜你喜欢
	searchRecommend: function(){
		var that = this;
		app.apiGet(app.apiList.searchRecommend,{},function(res){
			if(res.code == 200){
				that.setData({
					guessLike:res.data.flag
				})
			}
		})
	},
	
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		if (this.data.nextPageUrl) {
      var typeloading = 'onReachBottom';
      this.searchConfirm(typeloading);
		}
	},
	//选择城市
	cityTap: function () {
		wx.navigateTo({
			url: '/pages/switchCity/switchCity?city=' + this.data.city,
		})
	},
	//搜索
	searchInput: function (e) {
		this.setData({
			searchVal: e.detail.value
		})
	},
	//搜索获取光标
	searchFocus: function (e) {
		this.setData({
			iconClose: false
		})
	},
	//搜索失去光标
	searchBlur: function(){
		this.setData({
			iconClose: true
		})
	},
	//clear
	clearTap: function(){
		this.setData({
			searchVal:''
		})
	},
	//确认搜索
  searchConfirm: function (typeloading){
		var that = this;
    //判断来至滚动加载翻页
    if (typeloading != 'onReachBottom'){
        this.data.pageNum = 0;
        this.data.positions =[];
        this.setData({ scrollTop:0})
    }
		app.loading();
		app.apiPost(app.apiList.search,{
			city: this.data.city =='全国'?'':this.data.city,
			name: this.data.searchVal,
			page: this.data.pageNum
		},function(res){
			app.hideloading();
			if (res.code == 200 && res.data.positions.length>0){
				let newpositions = that.data.positions.concat(res.data.positions);
				let newpageNum = ++that.data.pageNum;
				that.setData({
					positions: newpositions,
					nextPageUrl: res.data.next_page_url,
					pageNum: newpageNum,
					backgroundColor: '#f0eff5',
					likeHidden: true,
					nullHidden: true,
				})
			}else{
				that.setData({
					positions:[],
					nextPageUrl: res.data.next_page_url,
					nullHidden:false,
					backgroundColor: '#f0eff5',
					likeHidden: true
				})
			}
		})
		
	},
	//选中猜你喜欢
	checkLikeTap: function(e){
		this.setData({
			searchVal: e.currentTarget.dataset.value,
			searchFocus: true
		})
	},
	//进入详情页面
	positionDetailTap: function (e) {
		var id = e.currentTarget.dataset.positionId;
		if(id){
			wx.navigateTo({
				url: '/pages/detail/detail?id=' + id,
			})
		}
		
	},
	//share 统一分享
	onShareAppMessage: function (res) {
		if (res.from === 'button') {
			// 来自页面内转发按钮
			console.log(res.target)
		}
		return {
			title: '财咖｜会计招聘找工作',
			path: '/pages/index/index',
			imageUrl: '/images/share.jpg',
			success: function (res) {
				// 转发成功
			},
			fail: function (res) {
				// 转发失败
			}
		}
	},
})